import { ATeamCombat } from "GameLoop/WorldState/EntityManager/Entitys";
import { PositionHelper } from "utils/position/PositionHelper";
import { IDefendStrategy } from "./Base";
import { destoryPriority } from "utils/constants/priorityconstant";
import { CreepAbilityHelper } from "utils/creep/CreepAbilityHelper";
import { ETaskState } from "GameLoop/DecisionMaking/TaskDeclare/type";

/**
 * 没有ram的房间进攻
 */
export class DefendStrategy2 extends IDefendStrategy
{
    // 这里应该以是否抵达房间为区分条件
    public assignTarget(room:Room,task:ITask<ITeamTaskData<"defend">>,teams:ATeamCombat[],creeps:Creep[]): ETaskState 
    {
        const workroom = Game.rooms[task.data.room];
          // 设置移动目标,测试
        const goal = this.getMoveGoal(task.data.room,workroom);

        if(workroom)
        {
            const cachehostiles = workroom.getWorld().PollHostiles(workroom.name);
            if(!cachehostiles)
            {
                // 错误，没有获取道敌方单位
                room.log("error","在拥有视野的情况下没有获取道敌方爬的缓存",workroom.name);
                return    ETaskState.Running;
            }
            const enemy:Creep[] = [];
            for(const creepid of cachehostiles.cacheData)
            {
                const e = Game.getObjectById(creepid.id);
                if(e)
                {
                    enemy.push(e);
                }
            }
            // 同时对房间环境进行分析
            // const groups = WarStrategy.classifyEnemy(enemy);
          
            task.data.arg.enemy =   enemy.map(e=>e.id);
            task.data.arg.structures = task.data.arg.structures.filter(e=>Game.getObjectById(e));
            if( task.data.arg.structures .length==0 && !workroom.IsMy())
            {
                
                const sstructures = _.sortBy(
                    workroom.find(FIND_STRUCTURES).filter(e=>e.structureType != STRUCTURE_ROAD && e.structureType != STRUCTURE_CONTROLLER && e.structureType != STRUCTURE_KEEPER_LAIR && e.structureType != STRUCTURE_CONTAINER),
                    e=>
                    {
                        const p = destoryPriority[e.structureType];
                        if(p)
                        {
                            return p
                        }
                        return 20
                    }
                );
                task.data.arg.structures  = sstructures.map(e=>e.id);
            }
            this.updateTeamAroundHostile(teams,enemy);
            this.updateCreepAroundHostile(creeps,enemy);
            if(task.data.arg.structures.length>0) 
            {
                this.setCreepTactic(creeps,{goal:goal,type:2,targets:[PositionHelper.getObjectRef(task.data.arg.structures[0])]},task.data.room);
                this.setTeamTractic(teams,{goal:goal,type:2,targets:[PositionHelper.getObjectRef(task.data.arg.structures[0])]},task.data.room);
            }else if(task.data.arg.enemy.length>0)
            {
                const enemy_target = _.max(enemy,e=>
                    {
                        if(PositionHelper.isInExit(e.pos))
                        {
                            return Math.round( (e.hitsMax+Math.max(e.hitsMax-e.hits))*0.1)
                        }
                        return e.hitsMax+Math.max(e.hitsMax-e.hits)
                    }
                    )
                this.setCreepTactic(creeps,{goal:goal,type:2,targets:[PositionHelper.getObjectRef(enemy_target)]},task.data.room,this.correctCreep);
                this.setTeamTractic(teams,{goal:goal,type:2,targets:[PositionHelper.getObjectRef(enemy_target)]},task.data.room,this.correctTeam);
            }  
            else 
            {
                // 没有攻击目标
                if(workroom.controller)
                {
                    const roomgoal = {
                        pos:workroom.controller!.pos,
                        range:4
                    }
                    this.setCreepTactic(creeps,{goal:roomgoal,type:2,targets:[]},task.data.room);
                    this.setTeamTractic(teams,{goal:roomgoal,type:2,targets:[]},task.data.room);
                }
                
                let idx = task.data.arg.benchRooms.findIndex(e=>e == task.data.room);
                if(idx > -1)
                {
                    // 
                    idx++;
                }
                else 
                {
                    idx=0;
                }
                // 还有没有待检测的房间
                if(task.data.arg.benchRooms.length)
                {
                    // 有
                    task.data.room = task.data.arg.benchRooms[idx%task.data.arg.benchRooms.length];
                }
            }
            
            // this.deployStrategy1(teams,task.data);
        }
        else 
        {
            // 没有视野
            this.setCreepTactic(creeps,{goal:goal,type:2,targets:[]},task.data.room);
            this.setTeamTractic(teams,{goal:goal,type:2,targets:[]},task.data.room);
        }
        return ETaskState.Running
    }
}
